Skip to content

internal/fakecgo: reserve threadentry's spill slot on amd64 - #568

Merged
hajimehoshi merged 4 commits into
ebitengine:mainfrom
AnyCPU:fix/trampoline_1
Nov 23, 2026
Merged

hajimehoshi merged 4 commits into
ebitengine:mainfrom
AnyCPU:fix/trampoline_1

Conversation

@AnyCPU

@AnyCPU AnyCPU commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What issue is this addressing?

On darwin/amd64, CI's CGO_ENABLED=0 go test "-gcflags=all=-N -l" ./... hangs in internal/fakecgo's TestThreadEntryReturn (added in #486). It reproduces on main (cfb65d5) with Go 1.27.4 and 1.25.0:

CGO_ENABLED=0 go test "-gcflags=all=-N -l" -run '^TestThreadEntryReturn$' -count=1 ./internal/fakecgo/

Optimized builds and cgo builds pass. CI's macos-latest runner is arm64, which is why CI hasn't caught it.

What type of issue is this addressing?

updates #571.

What this PR does | solves

Cause. threadentry is an ABIInternal function with one register argument, and the ABI requires its caller
to reserve that argument's spill slot ("the caller also reserves spill space on the stack for all register-based
arguments", cmd/compile/abi-internal.md). threadentry_trampoline reserves none. The slot is then the
trampoline's 0(SP), where PUSH_REGS_HOST_TO_ABI0() saved BX. Built with -N, threadentry stores its
argument there (MOVQ AX, 0x48(SP) after a 0x40-byte frame), and POP_REGS_HOST_TO_ABI0() hands the C caller
the freed ThreadStart pointer in BX.

macOS's _pthread_start keeps the thread's pthread_t in rbx across the start routine and passes it to
_pthread_exit afterwards. _pthread_terminate then faults on the bogus pthread_t while holding_pthread_list_lock. The thread has no g, so the fault repeats at full CPU, every later pthread_create blocks on the lock, no new M starts, and the test waits forever. The same clobber happens on the other amd64 systems, but glibc doesn't read the thread from BX after the start routine returns, which would explain why Linux CI passes.

This is the thread-exit path of #485 (fixed by #486), with a different register.

Fix. Reserve the slot with ADJSP around the call, after the registers are pushed, as runtime·sigtramp does before calling sigtrampgo ("Reserve space for spill slots", runtime/sys_darwin_amd64.s).

  • A larger declared frame, as the simpler trampolines in the file use, wouldn't help here: the frame is set up
    before PUSH_REGS_HOST_TO_ABI0(), and the slot at the CALL would still be the saved BX.
  • ADJSP rather than SUBQ/ADDQ: the assembler tracks SP only through PUSH/POP/ADJSP, and SUBQ $n, SP would mark the trampoline SPWRITE.

Test. TestThreadEntryTrampolinePreservesBX catches the bug on every amd64 system in every build mode, not only unoptimized builds on darwin/amd64.

  • It swaps threadentry_call for a probe that takes the address of its argument, which makes even an optimized
    build store it in its spill slot.
  • It calls the trampoline through a small asm helper, callThreadEntryTrampoline, that puts a known value in BX and returns BX afterwards.
  • The probe forwards any other call to the real threadentry, which keeps threads the runtime starts during the
    test working. Like threadentry, it is nosplit and norace.
  • A second check guards against a vacuous pass: the probe records the word above its slot, which must be the saved BX.
  • The helper's Go declaration is in threadentry_amd64.go, because vet's asmdecl checks the non-test package.
    The linker drops it from normal binaries.

Testing.

  • darwin/amd64 (Intel, macOS 15.8.1), Go 1.27.4 and 1.25.0:
    • CI's macOS commands all pass: vet, build, -shuffle=on -count=10 with and without cgo, -gcflags=all=-N -l with and without cgo, -race -shuffle=on -count=10 with and without cgo.
    • Without the fix, the new test fails in default, -N -l and -race builds (BX equals the argument). With it, it passes.
  • linux/amd64 (Docker, glibc 2.41, Go 1.27.3):
    • Without the fix, the new test fails in default and -N -l builds, while TestThreadEntryReturn passes.
    • With the fix, the suite passes, including -N -l.
  • netbsd and illumos amd64: vet clean. freebsd/amd64: the test binary builds with CI's -std flag.

Notes.

  • A macos-15-intel runner would cover darwin/amd64 in CI, if you'd like one.
  • go test -gcflags=-N (package only, not all=) fails to link with a nosplit stack overflow in x_cgo_thread_start. It fails the same way on main and is unrelated to this change.
  • Found by reading, not run or changed here: on arm, threadentry_trampoline saves R4 at 8(R13), which is ABI0 threadentry's result slot, and return nil restores R4 = 0 to libc in every build. On ppc64le, SAVE_GPR(32) puts R14 in threadentry's spill slot with -N, which is latent today because -N builds don't link there.

@AnyCPU

AnyCPU commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

TL;DR

I was working on bindings based on purego. I was interested in fast C->Go calls, so I came up with a solution very similar to the one in #562. While testing #562, I found the bug that this PR fixes: thread exit hangs debug builds on Intel Macs.

@hajimehoshi

Copy link
Copy Markdown
Member

The arm and ppc64le overlaps from the Notes reproduce under qemu-user 10.0.13 with glibc 2.41:

  • arm: a probe that seeds R4 and calls threadentry_trampoline gets R4 = 0 back in GOARM=5, GOARM=7 and GOARM=7 -gcflags=all=-N -l builds.
  • ppc64le: a probe that takes the address of v, as in this PR's test, gets R14 = v back.

Neither shows up as a crash in TestThreadEntryReturn-style thread exits under qemu.

Would you like to include these in this PR, or should they go in a separate one?

Reproduced by Claude (Claude Code).

@hajimehoshi hajimehoshi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted by Claude (Claude Code) on behalf of hajimehoshi. All of these are suggestions.

Comment thread internal/fakecgo/trampolines_amd64.s Resolved
@AnyCPU

AnyCPU commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@hajimehoshi hey, thanks for the review, all suggestions are applied.

i'd keep arm and ppc64le out of this mr.

@hajimehoshi

Copy link
Copy Markdown
Member

Can you file an issue for this and for related PRs? Thanks,

@AnyCPU

AnyCPU commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@hajimehoshi done

#571 covers this one and the both follow-ups (#569, #570).

@hajimehoshi hajimehoshi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TotallyGamerJet PTAL

@TotallyGamerJet TotallyGamerJet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks good. I don't think we actually need the test for this though. It adds more assembly and a complicated looking test when the change itself is 4 lines. I'd prefer to have it removed but willing to keep it if @hajimehoshi prefers

@hajimehoshi

Copy link
Copy Markdown
Member

I'm leaning toward removing tests. @AnyCPU Could you remove the test file? Thanks,

@hajimehoshi
hajimehoshi merged commit 960caea into ebitengine:main Nov 23, 2026
53 of 54 checks passed
@AnyCPU
AnyCPU deleted the fix/trampoline_1 branch November 23, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants